package com.itcast.plane.modoule.indent.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itcast.plane.modoule.company.domain.Company;
import com.itcast.plane.modoule.flight.domain.Flight;
import com.itcast.plane.modoule.indent.domain.Indent;
import com.itcast.plane.modoule.indent.dto.*;
import com.itcast.plane.modoule.indent.mapper.IndentMapper;
import com.itcast.plane.modoule.path.domain.Path;
import com.itcast.plane.modoule.person.domain.Person;
import com.itcast.plane.modoule.planes.domain.Planes;
import com.itcast.plane.modoule.ticket.domain.Ticket;
import com.itcast.plane.util.CommonService;
import com.itcast.plane.util.JsonResult;
import com.itcast.plane.util.JsonUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class IndentService extends ServiceImpl<IndentMapper, Indent> {

    @Resource
    private IndentMapper indentMapper;

    @Resource
    private CommonService commonService;

    public JsonResult order(IndentCreatDTO creatDTO) {
        List<IndentGetPersonIdEntity> personIdList = JsonUtil.getPersonIdList(creatDTO.getPerson());
        LambdaQueryWrapper<Indent> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        Flight flight = commonService.getFlightById(creatDTO.getFlightId());
        Ticket ticket = commonService.getTicketById(flight.getTicketId());
        if (personIdList.size() != creatDTO.getNumber()) {
            return new JsonResult(false, "下单失败,下单人数超限");
        }
        List<Map> returnList = new ArrayList<>();
        int num = 0;
        for (int i = 0; i < personIdList.size(); i++) {
            int personId = personIdList.get(i).getId();
            lambdaQueryWrapper = new LambdaQueryWrapper<>();
            lambdaQueryWrapper.eq(Indent::getFlightId, creatDTO.getFlightId()).eq(Indent::getPersonId, personId);
            Indent indent = indentMapper.selectOne(lambdaQueryWrapper);
            if (!ObjectUtils.isEmpty(indent)) {
                num++;
                Map<String, String> map = new HashMap<>();
                map.put("name",commonService.getPersonById(personId).getName());
                returnList.add(map);
            }
        }
        lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Indent::getFlightId,creatDTO.getFlightId()).eq(Indent::getSeatInfo,creatDTO.getSeatInfo());
        int count = indentMapper.selectCount(lambdaQueryWrapper);
        if (num == 0) {
            Map<String, Integer> map = new HashMap<>();
            for (int i = 0; i < personIdList.size(); i++) {
                Indent indent1 = new Indent();
                BeanUtils.copyProperties(creatDTO, indent1);
                boolean b = ticketNum(indent1.getFlightId(),1,creatDTO.getSeatInfo());
                if (!b){
                    IndentPayDTO payDTO = new IndentPayDTO();
                    payDTO.setUserId(creatDTO.getUserId());
                    payDTO.setIndentIds(map.toString());
                    payDTO.setType(3);
                    pay(payDTO);
                    return new JsonResult(false,"没有足够的票了");
                }
                indent1.setPersonId(personIdList.get(i).getId());
                indent1.setSeat(creatDTO.getSeatInfo()+(count+i+1)+"号座位");
                indentMapper.insert(indent1);
                map.put("id",indent1.getId());
                returnList.add(map);
            }
            return new JsonResult(true,"下单成功请支付",returnList);
        }else {
            return new JsonResult(false,"有乘客已经购买过票",returnList);
        }
    }

    public JsonResult pay(IndentPayDTO dto) {
        List<IndentGetPersonIdEntity> list = JsonUtil.getPersonIdList(dto.getIndentIds());
        for (int i = 0; i < list.size(); i++) {
            Indent indent = indentMapper.selectById(list.get(i).getId());
            if (dto.getType() == 1){
                indent.setPay(1);
                indentMapper.updateById(indent);
                if (i == list.size() - 1){
                    return new JsonResult(true,"支付成功");
                }
            }else if (dto.getType() == 2){
                ticketNum(indent.getFlightId(),2,indent.getSeatInfo());
                indent.setPay(2);
                indentMapper.updateById(indent);
                if (i == list.size() - 1){
                    return new JsonResult(true,"退款成功");
                }
            }else if (dto.getType() == 3){
                ticketNum(indent.getFlightId(),2,indent.getSeatInfo());
                indentMapper.deleteById(list.get(i).getId());
                if (i == list.size() -1){
                    return new JsonResult(true,"订单已取消");
                }
            }else {
                return new JsonResult(false,"无效操作");
            }
        }
        return null;
    }

    public JsonResult getPage(IndentLookDTO lookDTO) {
        LambdaQueryWrapper<Indent> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Indent::getUserId,lookDTO.getUserId()).eq(Indent::getPay,lookDTO.getType());
        List<Indent> indentList = indentMapper.selectList(lambdaQueryWrapper);
        if (indentList.size() == 0){
            return new JsonResult(false,"没有订单信息");
        }
        return getIndentReturn(indentList);
    }

    private JsonResult getIndentReturn(List<Indent> indentList) {
        List<IndentBackVO> list = new ArrayList<>();
        IndentBackVO vo;
        for (Indent indent: indentList) {
            vo = new IndentBackVO();
            Flight flight = commonService.getFlightById(indent.getFlightId());
            Planes planes = commonService.getPlanesById(flight.getPlaneId());
            Company company = commonService.getCompanyById(flight.getCompId());
            Path path = commonService.getPathById(flight.getPathId());
            Person person = commonService.getPersonById(indent.getPersonId());
            BeanUtils.copyProperties(path,vo);
            BeanUtils.copyProperties(person,vo);
            BeanUtils.copyProperties(flight,vo);
            vo.setPlaneName(planes.getName());
            vo.setCompName(company.getName());
            BeanUtils.copyProperties(indent,vo);
            list.add(vo);
        }
        return new JsonResult(true,list);
    }

    public JsonResult getPerson(IndentLookPersonDTO lookPersonDTO) {
        LambdaQueryWrapper<Person> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Person::getIdCard,lookPersonDTO.getIdCard()).eq(Person::getName,lookPersonDTO.getName());
        List<Person> personList = commonService.getPersonList(lambdaQueryWrapper);
        List<Integer> personIds = new ArrayList<>();
        for (Person p : personList) {
            personIds.add(p.getId());
        }
        LambdaQueryWrapper<Indent> indentLambdaQueryWrapper = new LambdaQueryWrapper<>();
        if (personIds.size() == 0){
            return new JsonResult(false,"未找到您的用户信息");
        }
        if (personIds.size()>1) {
            indentLambdaQueryWrapper.in(Indent::getPersonId, personIds);
        }else {
            indentLambdaQueryWrapper.eq(Indent::getPersonId,personIds.get(0));
        }
        indentLambdaQueryWrapper.eq(Indent::getPay,1);
        List<Indent> list = indentMapper.selectList(indentLambdaQueryWrapper);
        if (list.size() == 0){
            return new JsonResult(false,"没有订单信息");
        }
        return getIndentReturn(list);
    }

    private boolean ticketNum(int flightid,int type,String seatInfo){
        Flight flight = commonService.getFlightById(flightid);
        Ticket ticket = commonService.getTicketById(flight.getTicketId());
        int seat = 0;
        if ("商务舱".equals(seatInfo)){
            seat = 2;
        }else if ("头等舱".equals(seatInfo)){
            seat = 1;
        }else {
            seat = 3;
        }

        if (type == 1){
            if (seat == 1){
                if (ticket.getFirstNum() == 0){
                    return false;
                }
                ticket.setFirstNum(ticket.getFirstNum() - 1);
                commonService.editTicket(ticket);
            }else if (seat == 2){
                if (ticket.getSecondNum() == 0){
                    return false;
                }
                ticket.setSecondNum(ticket.getSecondNum() - 1);
                commonService.editTicket(ticket);
            }else {
                if (ticket.getThirdNum() == 0){
                    return false;
                }
                ticket.setThirdNum(ticket.getThirdNum() - 1);
                commonService.editTicket(ticket);
            }
        }else {
            if (seat == 1){
                ticket.setFirstNum(ticket.getFirstNum() + 1);
                commonService.editTicket(ticket);
            }else if (seat == 2){
                ticket.setSecondNum(ticket.getSecondNum() + 1);
                commonService.editTicket(ticket);
            }else {
                ticket.setThirdNum(ticket.getThirdNum() + 1);
                commonService.editTicket(ticket);
            }
        }
        return true;
    }
}
